<?php

namespace Admin\Controller;
use Think\Model;
use Org\Util\Sync;
use User\Api\UserApi;

/**
 * 礼品卡控制器
 * @author wangyuanlin
 * 2015-06-17
 */
class GiftCardController extends AdminController {

    /**
     * 首页
     * @author wangyuanlin
     * 2015-06-17
     */
    public function index() {
        
        $title = I('title');
        $map['batchNo'] = array('like', '%' . (string) $title . '%');
        $list = $this->lists('vgiftcard', $map,'batchNo desc');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }
    
    /**
     * 生成礼品卡
     * @author wangyuanlin
     * 2015-06-17
     */
    public function add($amount = "", $cnt = "") {
        if (IS_POST) {
            //批次名称
            $batchNo = date("ymdHis").'-'.$amount;
            
            for($i = 0;$i<$cnt;$i++){
                //密钥
                $code = substr(md5(NOW_TIME.$i),-10);
                $data = array('batchNo' => $batchNo, 'amount' => $amount, 'code' => $code, 'createdate' => NOW_TIME, 'lastupdate' => NOW_TIME);
                $id = M('giftcard')->add($data);
                if (!$id) {
                    $this->error("生成失败！");
                }
            }
            $this->success('生成成功！', U('index'));
        } else {
            $this->display();
        }
    }
    
    /**
     * 导出
     * @author wangyuanlin
     * 2015-06-19
     */
    public function export(){
        
        $batchNo = I('batchNo'); 
        
         $sql = "select a.batchNo,a.amount,a.code
            from gr_giftcard a 
            where a.batchNo = '".$batchNo."'
            order by createdate,id asc";
         $Model = new Model();         
         $data = $Model->query($sql);

        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");

        $filename="礼品卡-".$batchNo;
        
        $headArr=array("批次名称","金额","密钥");
        $title = "礼品卡列表";
        $this->getExcel($filename,$headArr,$data,$title);
    }
    
    

    private function getExcel($fileName,$headArr,$data,$title){
         //对数据进行检验
        if(empty($data) || !is_array($data)){
            //die("data must be a array");
        }
        //检查文件名
        if(empty($fileName)){
            exit;
        }
        // H:i:s
        $date = date("Y_m_d_H_i",time());
        //$date = date("Y_m_d",time());
        $fileName .= "_{$date}.xls";
        
        //创建PHPExcel对象，注意，不能少了\
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();
        
        //第一列设置报表时间范围
        $key = ord("A");
        $colum = chr($key);
        $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $title);
        
        //第二列设置报表列头
        $key = ord("A");
        foreach($headArr as $v){
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'2', $v);
            $key += 1;
        }
        
        $column = 3;
        $objActSheet = $objPHPExcel->getActiveSheet();
        foreach($data as $key => $rows){ //行写入
            $span = ord("A");
            foreach($rows as $keyName=>$value){// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j.$column, $value);
                $span++;
            }
            $column++;
        }

        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
            // $objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
            header("Content-Disposition: attachment;filename=\"$fileName\"");
            header('Cache-Control: max-age=0');

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        exit;
    }
    
    /**
     * 上推
     * @author wangyuanlin
     * 2015-06-23
     */
    public function push(){
       $reuslt = D("GiftcardLeanCloud")->push();
       $this->success('礼品卡数据上推成功！', U('index'),true);
    }

}
